<?php

function delete_article($aids) {
	
	if(is_array($aids)) {
		$aids = dimplode($aids);
	}
	
	$catids = DB::getCol("SELECT catid FROM ".DB::table('article_index')." WHERE aid IN($ids)");
	
	DB::delete('article', "aid IN($aids)");
	DB::delete('article_index', "aid IN($aids)");
	update_articlecat($catids);
	delete_article_attach($aids);
	
}

function update_articlecat($catids) {
	foreach($catids as $catid) {
		$count = DB::getOne('SELECT COUNT(*) FROM '.DB::table('article_index')." WHERE catid='$catid'");
		DB::query('UPDATE '.DB::table('articlecat')." SET articles='{$count}' WHERE catid='$catid'");
	}
}

function delete_article_attach($aids) {
	
	$query = DB::query("SELECT * FROM ".DB::table('article_attachment')." WHERE aid IN ($aids)");
	while ($value = DB::fetch($query)) {
		$attachment[] = $value;
		$attachdel[] = $value['attachid'];
	}
	
	foreach ($attachment as $value) {
		delete_attach($value['attachment'], 'article', $value['thumb'], $value['remote']);
	}
	DB::delete('article_attachment', "aid IN ($aids)");
	
}

function clear_trash() {
	$aids = DB::getCol("SELECT aid FROM ".DB::table('article_trash'));
	$aids = dimplode($aids);
	DB::delete('article_trash', "aid IN($aids)");
	delete_article_attach($aids);
}

function delete_trash_article($aids) {
	
	if(is_array($aids)) {
		$aids = dimplode($aids);
	}
	
	DB::delete('article_trash', "aid IN($aids)");
	delete_article_attach($aids);
}

?>